//
//  LVThreeBtnTool.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/2/7.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVThreeBtnTool.h"
#import "UIView+rotateLayerAnimation.h"

@implementation LVThreeBtnTool
{
    UIButton *_leftBtn;
    UIButton *_centerBtn;
    UIButton *_rigthBtn;
    UIView *_showAtView;
    UIView *_scrollLine;
    UIImageView *_bgImgView;
    id <LVThreeBtnToolDelegate> _delegate;
    BOOL _isOnlyHideMiddleBtn;
}
+ (id)shareThreeBtn
{
    static LVThreeBtnTool *shareThreeBtn = nil;
    static dispatch_once_t pre;
    dispatch_once(&pre, ^{
        shareThreeBtn = [[LVThreeBtnTool alloc] init];
    });

    return shareThreeBtn;
}
#pragma mark createUI
- (void)createThreeBtnAtView:(UIView *)view delegate:(id<LVThreeBtnToolDelegate>)delegate
{
    UIImageView *bgImgView = [[UIImageView alloc] init];
    bgImgView.left = view.left;
    bgImgView.height = 60;
    bgImgView.width = view.width;
    bgImgView.top = view.height - 60;
    bgImgView.image = [UIImage imageNamed:@"im_home_bottom_bj"];
    bgImgView.hidden = YES;
    _bgImgView = bgImgView;
    bgImgView.userInteractionEnabled = YES;
    [view addSubview:bgImgView];

    UIColor *clearColor = [UIColor clearColor];

    UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _centerBtn = centerBtn;
    
    [centerBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:centerBtn];

    UIView *scrollLine = [[UIView alloc] init];
    scrollLine.backgroundColor = KMainColor;
    scrollLine.layer.masksToBounds = YES;
    scrollLine.layer.cornerRadius = 1.5;
    [view addSubview:scrollLine];
    _scrollLine = scrollLine;

    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.backgroundColor = clearColor;
    [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    _leftBtn = leftBtn;
    [view addSubview:leftBtn];

    UIButton *rigthBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rigthBtn.backgroundColor = clearColor;
    [rigthBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    _rigthBtn = rigthBtn;
    [view addSubview:rigthBtn];

    _showAtView = view;
    _delegate = delegate;

    //默认初始化就是如此
    [self layoutBtnDirection:LVThreeBtnCenter];
    [self layoutScrollDirection:LVThreeBtnCenter];
    [self showBtnBackgroundImageDirection:LVThreeBtnCenter];
}
//底部滚动条滚动
- (void)scrollToDirection:(LVThreeBtnDirection)dir
{
    WeakSelf(weakSelf);
    [UIView animateWithDuration:0.5f animations:^{
        [weakSelf layoutScrollDirection:dir];
        [weakSelf showBtnBackgroundImageDirection:dir];
    }];
}
#pragma mark layout
- (void)layoutBtnDirection:(LVThreeBtnDirection)dir
{
    CGFloat centerBtnWidth;
    CGFloat leftBtnWidth;
    CGFloat rigthBtnWidth;
    switch (dir) {
        case LVThreeBtnLeft:
        case LVThreeBtnRigth:
        {
            centerBtnWidth  = 50;
            leftBtnWidth = 50;
            rigthBtnWidth = 50;
        }
            break;
        case LVThreeBtnCenter:
        {
            centerBtnWidth = 84;
            leftBtnWidth = 50;
            rigthBtnWidth = 50;
        }
            break;
        default:
            break;
    }
    _centerBtn.width = centerBtnWidth;
    _centerBtn.height = centerBtnWidth;
    _centerBtn.centerX = _showAtView.centerX;
    _centerBtn.y = _showAtView.height - 10 - centerBtnWidth;

    _leftBtn.width = leftBtnWidth;
    _leftBtn.height = leftBtnWidth;
    _leftBtn.y = _showAtView.height - 10 - leftBtnWidth;

    _rigthBtn.width = rigthBtnWidth;
    _rigthBtn.height = rigthBtnWidth;
    _rigthBtn.y = _showAtView.height - 10 - rigthBtnWidth;

    switch (dir) {
        case LVThreeBtnLeft:
        case LVThreeBtnRigth:
        {
            _leftBtn.left = _showAtView.left + 45;
            _rigthBtn.right = _showAtView.right - 45;
        }
            break;
        case LVThreeBtnCenter:
        {
            _leftBtn.left = _showAtView.left + 10;
            _rigthBtn.right = _showAtView.right - 10;
        }
            break;
        default:
            break;
    }
}
- (void)layoutScrollDirection:(LVThreeBtnDirection)dir
{
    CGFloat width;
    CGFloat top;
    CGFloat centerX;
    switch (dir) {
        case LVThreeBtnLeft:
            _scrollLine.hidden = NO;
            width = 30;
            top = _leftBtn.bottom;
            centerX = _leftBtn.centerX;
            break;
        case LVThreeBtnCenter:
            _scrollLine.hidden = YES;
            width = 64;
            top = _leftBtn.bottom;
            centerX = _centerBtn.centerX;
            break;
        case LVThreeBtnRigth:
            _scrollLine.hidden = NO;
            width = 30;
            top = _rigthBtn.bottom;
            centerX = _rigthBtn.centerX;
            break;
        default:
            break;
    }
    _scrollLine.width = width;
    _scrollLine.height = 3;
    _scrollLine.top = top;
    _scrollLine.centerX = centerX;
}
#pragma mark showBtn
- (void)showBtnBackgroundImageDirection:(LVThreeBtnDirection)dir
{
    switch (dir) {
        case LVThreeBtnLeft:
        {
            [_centerBtn setBackgroundImage:[UIImage imageNamed:@"im_home_circular"] forState:UIControlStateNormal];
            [_leftBtn setImage:[UIImage imageNamed:@"im_dialogue"] forState:UIControlStateNormal];
            [_rigthBtn setImage:[UIImage imageNamed:@"im_mail"] forState:UIControlStateNormal];
        }
            break;
        case LVThreeBtnCenter:
        {
            [_centerBtn setBackgroundImage:[UIImage imageNamed:@"greet_gesture"] forState:UIControlStateNormal];
            [_leftBtn setImage:[UIImage imageNamed:@"greet_dialogue"] forState:UIControlStateNormal];
            [_rigthBtn setImage:[UIImage imageNamed:@"greet_contacts"] forState:UIControlStateNormal];
        }

            break;
        case LVThreeBtnRigth:
        {
            [_centerBtn setBackgroundImage:[UIImage imageNamed:@"im_home_circular"] forState:UIControlStateNormal];
            [_leftBtn setImage:[UIImage imageNamed:@"menu_dialogue"] forState:UIControlStateNormal];
            [_rigthBtn setImage:[UIImage imageNamed:@"im_mail_press"] forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
}
- (void)showInTheMiddle
{
    if (!_isOnlyHideMiddleBtn) {
        _centerBtn.hidden = NO;
    }else{
        _centerBtn.hidden = YES;
    }

    _leftBtn.hidden = NO;
    _rigthBtn.hidden = NO;
    _bgImgView.hidden = YES;

    WeakSelf(weakSelf);
    [UIView animateWithDuration:0.5f animations:^{
        [weakSelf layoutBtnDirection:LVThreeBtnCenter];
    }completion:^(BOOL finished) {
    }];
}
- (void)showInBothSides
{
    _centerBtn.hidden = NO;
    _leftBtn.hidden = NO;
    _rigthBtn.hidden = NO;
    _bgImgView.hidden = NO;

    WeakSelf(weakSelf);
    [UIView animateWithDuration:0.5f animations:^{
        [weakSelf layoutBtnDirection:LVThreeBtnLeft];
    }completion:^(BOOL finished) {

    }];
}
- (void)showOnlyMiddle
{
    _centerBtn.hidden = NO;
    _leftBtn.hidden = YES;
    _rigthBtn.hidden = YES;
    _bgImgView.hidden = YES;

    WeakSelf(weakSelf);
    [UIView animateWithDuration:0.5f animations:^{
        [weakSelf layoutScrollDirection:LVThreeBtnCenter];
    }completion:^(BOOL finished) {

    }];
}
- (void)showOtherViewUnderDirection:(LVThreeBtnDirection)dir distance:(CGFloat)distance
{
    __weak UIButton *btn;
    switch (dir) {
        case LVThreeBtnLeft:
        {
            btn = _leftBtn;
        }
            break;
        case LVThreeBtnCenter:
        {
            btn = _centerBtn;
        }
            break;
        case LVThreeBtnRigth:
        {
            btn = _rigthBtn;
        }
            break;
        default:
            break;
    }
    [UIView animateWithDuration:0.5f animations:^{
        btn.y = _showAtView.height - 10 - _centerBtn.width - distance;
    }completion:^(BOOL finished) {

    }];
}
#pragma mark clickBtnAction
- (void)clickAction:(UIButton *)btn
{
    if (_delegate && [_delegate respondsToSelector:@selector(clickedThreeBtnTool:direction:)]) {
        LVThreeBtnDirection dir;
        if (btn == _leftBtn) {
            dir = LVThreeBtnLeft;

        }else if (btn == _centerBtn){
            dir = LVThreeBtnCenter;

        }else{
            dir = LVThreeBtnRigth;
        }
        [_delegate clickedThreeBtnTool:self direction:dir];
    }
}
#pragma mark hideBtn
- (void)hideAllBtn:(BOOL)isHide
{
    _centerBtn.hidden = isHide;
    _leftBtn.hidden = isHide;
    _rigthBtn.hidden = isHide;
    _scrollLine.hidden = isHide;
    _bgImgView.hidden = isHide;
}
- (void)hideMiddleBtn:(BOOL)isHide
{
    _centerBtn.hidden = isHide;
    _bgImgView.hidden = isHide;
}
- (void)hideOnlyMiddleBtn:(BOOL)isHide
{
    _isOnlyHideMiddleBtn = isHide;
    _centerBtn.hidden = isHide;
}
- (CGFloat )centerBtnTop{

    return _centerBtn.top;
}
- (CGFloat )centerBHeight{
    return _centerBtn.height;
}
- (CGPoint )centerBtnCenter{
    return _centerBtn.center;
}
- (UIButton *)centerBtn{
    return _centerBtn;
}
- (void)performCenterBtnWaveAnimation{
    [_centerBtn performLayerAnimationWithLayerWidth:5.0 inset:2.0 color:[UIColor whiteColor]];
    
    self.isCenterBtnAnimated = YES;
}

- (void)removeCenterBtnWaveAnimation{
    [_centerBtn removeAnimationLayer];
    self.isCenterBtnAnimated = NO;
}

@end
